package com.cctc.rds.raft.sdk.client.rpc;

import com.cctc.rds.raft.core.node.NodeIpPort;
import com.cctc.rds.raft.core.rpc.RpcClientRouter;
import com.cctc.rds.raft.core.rpc.election.ElectionRpc;
import com.cctc.rds.raft.sdk.client.RaftClient;
import com.cctc.rds.raft.sdk.client.rpc.election.ElectionRpcRequestImpl;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ElectionRpcClientRouter implements RpcClientRouter<ElectionRpc> {
    private int nodeId;
    private RaftClient raftClient;

    @Override
    public ElectionRpc route(NodeIpPort ipPort) {
        if (ipPort.getNodeId() == this.nodeId)
            throw new RuntimeException("不允许向自己发送 RPC 请求！！！");
        return new ElectionRpcRequestImpl(raftClient.getNettyRpcClient(nodeId));
    }
}
